# -*- coding: UTF-8 -*

import gtk, gobject

class Uploading:
    def uploading(self):
        self.vbox = gtk.VBox()
        wait_label = gtk.Label(_("Uploading... Please wait..."))
        self.vbox.pack_start(wait_label)
        wait_label.show()
        self.progressbar = gtk.ProgressBar()
        self.vbox.pack_start(self.progressbar, expand=False)
        self.timer = gobject.timeout_add(100, self.__progress_timeout)
        self.progressbar.pulse()
        self.progressbar.show()
        self.vbox.show()
        return self.vbox
        
    def __progress_timeout(self):
        self.progressbar.pulse()
        return True
        
    def destroy(self):
        gobject.source_remove(self.timer)
        del self.vbox
        
class Entry:
    def make(self, label, value):
        link_Box = gtk.HBox(spacing=5)
        link_label = gtk.Label(label)
        link_Box.pack_start(link_label, False, False)
        link_label.show()
        link_entry = gtk.Entry()
        link_entry.set_text(value)
        link_entry.connect("button-press-event", self.select_and_copy)
        link_Box.pack_start(link_entry)
        link_entry.show()
        link_Box.show()
        return link_Box
        
    def select_and_copy(self, widget, event):
        if event.type == gtk.gdk.BUTTON_PRESS:
            if event.button == 1:
                widget.select_region(0, -1)
                widget.copy_clipboard()
                
class TextView:
    def __init__(self, clipboard):
        self.clipboard = clipboard

    def make(self, label, value):
        html_Box = gtk.VBox()
        html_label = gtk.Label(label)
        html_label.set_alignment(0, 0.5)
        html_Box.pack_start(html_label)
        html_label.show()
        html_textview = gtk.TextView()
        html_textview.set_wrap_mode(gtk.WRAP_CHAR)
        html_textview.set_size_request(300, 70)
        html_textbuffer = html_textview.get_buffer()
        html_textbuffer.set_text(value)
        html_textview.connect("button-press-event", self.select_and_copy)
        html_Box.pack_start(html_textview, False, False)
        html_textview.show()
        html_Box.show()
        return html_Box
        
    def select_and_copy(self, widget, event):
        if event.type == gtk.gdk.BUTTON_PRESS:
            if event.button == 1:
                buf = widget.get_buffer()
                start, end = buf.get_bounds()
                buf.select_range(start,end)
                buf.copy_clipboard(self.clipboard)
        
class ResultWindow:
    def __init__(self, codes):
        self.result_window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.result_window.set_title("Imageshack.us applet")
        self.result_window.connect("destroy", self.__destroy_result_window)
        self.codes = codes
        
    def uploading(self):
        self.uploadingobj = Uploading()
        self.vbox = self.uploadingobj.uploading()
        self.result_window.add(self.vbox)
        self.result_window.show()
        return self.result_window
 
    def make(self, obj):
        if self.__dict__.has_key('uploadingobj'):
            self.result_window.remove(self.vbox)
            self.uploadingobj.destroy()
            del self.uploadingobj
        
        clipboard = gtk.Clipboard(selection='CLIPBOARD')
        # Objects
        entryobj = Entry()
        textviewobj = TextView(clipboard)
        # Image
        vbox = gtk.VBox()
        image_Box = gtk.HBox(spacing=5)
        image = gtk.Image()
        image_Box.pack_start(image, False, False)
        image.show()
        links_Box = gtk.VBox()
        image_Box.pack_start(links_Box)
        links_Box.show()
        vbox.pack_start(image_Box, False, False)
        image_Box.show()
        # Link
        #link_Box = entryobj.make("Url:", obj.image_link)
        #links_Box.pack_start(link_Box, False, False)
        # Ad Link
        #ad_Box = entryobj.make("Ad Url:", obj.ad_link)
        #links_Box.pack_start(ad_Box, False, False)
        # Html
        #html_Box = textviewobj.make("Html code:", obj.html_code())
        #links_Box.pack_start(html_Box, False, False)
        # BBCode
        #bb_Box = textviewobj.make("Forum code:", obj.forum_code())
        #links_Box.pack_start(bb_Box, False, False)
        for c in self.codes.data:
            if c['show']:
                if c['display'] == 0:
                    links_Box.pack_start(
                        entryobj.make(c['label']+":", obj.code(c['code'])),
                        False, False)
                elif c['display'] == 1:
                    links_Box.pack_start(
                        textviewobj.make(c['label']+":", obj.code(c['code'])),
                        False, False)
        vbox.show()
        self.result_window.add(vbox)
        self.result_window.show()
        pbl = gtk.gdk.PixbufLoader()
        pbl.write(obj.get_trumb())
        pbl.close()
        image.set_from_pixbuf(pbl.get_pixbuf())
        return self.result_window
    
    def __destroy_result_window(self, event):
        self.result_window.destroy()
